package com.hymanting.flinkapi.datastream;

import org.apache.flink.streaming.api.functions.source.SourceFunction;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Author hxchen
 * @Date 2021/7/7
 */
public class MySource implements SourceFunction<String> {

    private long count = 1L;

    private boolean isRunning = true;

    @Override
    public void run(SourceContext ctx) throws Exception {
        while(isRunning) {
            List<String> stringList = new ArrayList<>();
            stringList.add("world");
            stringList.add("Flink");
            stringList.add("Stream");
            stringList.add("Batch");
            stringList.add("Table");
            stringList.add("SQL");
            stringList.add("hello");
            int size = stringList.size();
            int i = new Random().nextInt(size);
            ctx.collect(stringList.get(i));
            Thread.sleep(1000);
        }
    }

    @Override
    public void cancel() {
        isRunning = false;
    }
}
